package com.up.vms.application.service;

import com.up.vms.application.common.DomainUtils;
import com.up.vms.domain.module.ShortVideoDTO;
import com.up.vms.domain.module.VideoDTO;
import com.up.vms.domain.vm.entity.Category;
import com.up.vms.domain.vm.entity.ShortVideo;
import com.up.vms.domain.vm.entity.Video;
import com.up.vms.domain.vm.enums.ShortVideoStatus;
import com.up.vms.domain.vm.enums.VideoRecommendFlag;
import com.up.vms.domain.vm.enums.VideoStatus;
import com.up.vms.domain.vm.enums.VideoType;
import com.up.vms.domain.vm.service.CategoryService;
import com.up.vms.domain.vm.service.ShortVideoService;
import com.up.vms.domain.vm.service.VideoService;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
@Slf4j
public class VideoApplicationService {

    @Autowired
    private VideoService videoService;

    @Autowired
    private CategoryService categoryService;


    public GeneRespDTO saveVideo(Video video) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        Category category = categoryService.findCategoryById(video.getCategoryId());
        if (category != null) {
            video.setCategory(category.getCategory());
        }

        Video saveShort = null;
        if (CommonUtils.isNotNull(video.getId())) {
            saveShort = videoService.getById(video.getId());
            if (saveShort != null) {
                saveShort.setVideoName(video.getVideoName());
                saveShort.setVideoDesc(video.getVideoDesc());
                saveShort.setVideoUrl(video.getVideoUrl());

                saveShort.setCategoryId(video.getCategoryId());
                saveShort.setCategory(video.getCategory());
                saveShort.setShowMode(video.getShowMode());
                saveShort.setShowVideoMode(video.getShowVideoMode());
                saveShort.setShowImage(video.getShowImage());
                saveShort.setSorting(video.getSorting());
                saveShort.setRecommendFlag(video.getRecommendFlag());
                saveShort.setModified(new Date());
            } else {
                saveShort = video;
            }
        } else {
            saveShort = video;
            saveShort.setPlayTimes(0);
        }

        saveShort.setStatus(1);
        saveShort.setType(VideoType.VIDEO.code);

        if (CommonUtils.isNotNull(video.getShowImage())) {
            saveShort.setShowImage(video.getShowImage());
        } else {
            if (CommonUtils.isNotNull(video.getVideoUrl())) {
                saveShort.setShowImage(video.getVideoUrl() + "?vframe/jpg/offset/1/w/678/h/380");
            }
        }

        Video newVideo = videoService.save(saveShort);

        geneRespDTO.setData(newVideo);
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));

        return geneRespDTO;
    }

    public GeneRespDTO queryAppById(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        Video video = videoService.getById(id);
        videoService.addPlayTimes(id);
        if (video != null) {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(video);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }
        return geneRespDTO;
    }

    public GeneRespDTO queryById(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        Video video = videoService.getById(id);
        if (video != null) {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(video);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }
        return geneRespDTO;
    }

    public GeneRespDTO<HashMap> getVideoList(VideoDTO videoDTO) throws Exception {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        Page<Video> simpleVideos = null;
        if (CommonUtils.isNotNull(videoDTO.getRecommendFlag())) {
            simpleVideos = videoService.findAllByRecommendFlag(videoDTO);
        } else {
            simpleVideos = videoService.findAllByCase(videoDTO);
        }

        if (CommonUtils.isNotNull(simpleVideos)) {
            geneRespDTO = DomainUtils.convert(simpleVideos);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }

    public GeneRespDTO delete(List<String> idList) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : idList) {
            Long id = Long.parseLong(idStr);
            Video video = videoService.getById(id);
            if (CommonUtils.isNotNull(video)) {
                video.setStatus(0);
                videoService.save(video);
            }
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(idList);
        return geneRespDTO;
    }

    @Autowired
    private ShortVideoService shortVideoService;

    public GeneRespDTO<HashMap> getVideoListByCategoryId(VideoDTO videoDTO) throws Exception {
        Long categoryId = videoDTO.getCategoryId();
        if (CommonUtils.isNotNull(categoryId) && categoryId == -1L) {
            VideoDTO newVideo = new VideoDTO();
            newVideo.setRecommendFlag(VideoRecommendFlag.RECOMMEND.code);
            newVideo.setShowVideoMode(VideoStatus.SHOW.code);
            newVideo.setSize(videoDTO.getSize());
            newVideo.setPage(videoDTO.getPage());
            newVideo.setSortColumn("sorting");
            return this.getVideoList(newVideo);
        } else if (CommonUtils.isNotNull(categoryId) && categoryId == -2L) {
            GeneRespDTO geneRespDTO = null;
            ShortVideoDTO shortVideoDTO = new ShortVideoDTO();
            shortVideoDTO.setSize(videoDTO.getSize());
            shortVideoDTO.setPage(videoDTO.getPage());
            shortVideoDTO.setStatus(ShortVideoStatus.APPROVED.code);

            Page<ShortVideo> simpleShortVideos = shortVideoService.findAllByCase(shortVideoDTO);

            if (CommonUtils.isNotNull(simpleShortVideos)) {
                geneRespDTO = DomainUtils.convert(simpleShortVideos);
            } else {
                throw new Exception("没有记录");
            }

            return geneRespDTO;
        } else {
            return this.getVideoList(videoDTO);
        }
    }
}
